﻿using System;
using System.ComponentModel;
using System.IO;
using System.Runtime.Serialization;

namespace HalconDotNet
{
	/// <summary>Represents a generic instance of a handle.</summary>
	// Token: 0x0200003F RID: 63
	[Serializable]
	public class HHandle : HHandleBase, ISerializable, ICloneable
	{
		// Token: 0x060005C0 RID: 1472 RVA: 0x00020CE0 File Offset: 0x0001EEE0
		[EditorBrowsable(EditorBrowsableState.Never)]
		public HHandle() : base(HHandleBase.UNDEF)
		{
		}

		// Token: 0x060005C1 RID: 1473 RVA: 0x00020CED File Offset: 0x0001EEED
		[EditorBrowsable(EditorBrowsableState.Never)]
		public HHandle(IntPtr handle) : base(handle)
		{
			this.AssertSemType();
		}

		// Token: 0x060005C2 RID: 1474 RVA: 0x00020CFC File Offset: 0x0001EEFC
		[EditorBrowsable(EditorBrowsableState.Never)]
		public HHandle(HHandle handle) : base(handle)
		{
			this.AssertSemType();
		}

		// Token: 0x060005C3 RID: 1475 RVA: 0x00020D0B File Offset: 0x0001EF0B
		private void AssertSemType()
		{
		}

		// Token: 0x060005C4 RID: 1476 RVA: 0x00020D0D File Offset: 0x0001EF0D
		internal static int LoadNew(IntPtr proc, int parIndex, int err, out HHandle obj)
		{
			obj = new HHandle(HHandleBase.UNDEF);
			return obj.Load(proc, parIndex, err);
		}

		// Token: 0x060005C5 RID: 1477 RVA: 0x00020D28 File Offset: 0x0001EF28
		internal static int LoadNew(IntPtr proc, int parIndex, int err, out HHandle[] obj)
		{
			HTuple htuple;
			err = HTuple.LoadNew(proc, parIndex, err, out htuple);
			obj = new HHandle[htuple.Length];
			for (int i = 0; i < htuple.Length; i++)
			{
				obj[i] = new HHandle(HalconAPI.IsLegacyHandleMode() ? htuple[i].IP : htuple[i].H);
			}
			htuple.Dispose();
			return err;
		}

		// Token: 0x060005C6 RID: 1478 RVA: 0x00020D98 File Offset: 0x0001EF98
		void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
		{
			HSerializedItem hserializedItem = this.SerializeHandle();
			byte[] value = hserializedItem;
			hserializedItem.Dispose();
			info.AddValue("data", value, typeof(byte[]));
		}

		// Token: 0x060005C7 RID: 1479 RVA: 0x00020DD0 File Offset: 0x0001EFD0
		[EditorBrowsable(EditorBrowsableState.Never)]
		public HHandle(SerializationInfo info, StreamingContext context)
		{
			HSerializedItem hserializedItem = new HSerializedItem((byte[])info.GetValue("data", typeof(byte[])));
			this.DeserializeHandle(hserializedItem);
			hserializedItem.Dispose();
		}

		/// <summary>Serialize object to binary stream in HALCON format</summary>
		// Token: 0x060005C8 RID: 1480 RVA: 0x00020E10 File Offset: 0x0001F010
		public virtual void Serialize(Stream stream)
		{
			HSerializedItem hserializedItem = this.SerializeHandle();
			hserializedItem.Serialize(stream);
			hserializedItem.Dispose();
		}

		/// <summary>Deserialize object from binary stream in HALCON format</summary>
		// Token: 0x060005C9 RID: 1481 RVA: 0x00020E24 File Offset: 0x0001F024
		public static HHandle Deserialize(Stream stream)
		{
			HHandle hhandle = new HHandle();
			HSerializedItem hserializedItem = HSerializedItem.Deserialize(stream);
			hhandle.DeserializeHandle(hserializedItem);
			hserializedItem.Dispose();
			return hhandle;
		}

		// Token: 0x060005CA RID: 1482 RVA: 0x00020E4A File Offset: 0x0001F04A
		object ICloneable.Clone()
		{
			return this.Clone();
		}

		// Token: 0x060005CB RID: 1483 RVA: 0x00020E54 File Offset: 0x0001F054
		public HHandle Clone()
		{
			HSerializedItem hserializedItem = this.SerializeHandle();
			HHandle hhandle = new HHandle();
			hhandle.DeserializeHandle(hserializedItem);
			hserializedItem.Dispose();
			return hhandle;
		}

		/// <summary>
		///   Clear the content of a handle. 
		///   Instance represents: Handle to clear.
		/// </summary>
		// Token: 0x060005CC RID: 1484 RVA: 0x00020E7C File Offset: 0x0001F07C
		public void ClearHandle()
		{
			IntPtr proc = HalconAPI.PreCall(2134);
			base.Store(proc, 0);
			int procResult = HalconAPI.CallProcedure(proc);
			HalconAPI.PostCall(proc, procResult);
			GC.KeepAlive(this);
		}

		/// <summary>
		///   Deserialize a serialized item. 
		///   Modified instance represents: Handle containing the deserialized item.
		/// </summary>
		/// <param name="serializedItem">Handle containing the serialized item to be deserialized.</param>
		// Token: 0x060005CD RID: 1485 RVA: 0x00020EB0 File Offset: 0x0001F0B0
		public void DeserializeHandle(HSerializedItem serializedItem)
		{
			this.Dispose();
			IntPtr proc = HalconAPI.PreCall(2135);
			HalconAPI.Store(proc, 0, serializedItem);
			HalconAPI.InitOCT(proc, 0);
			int num = HalconAPI.CallProcedure(proc);
			num = base.Load(proc, 0, num);
			HalconAPI.PostCall(proc, num);
			GC.KeepAlive(this);
			GC.KeepAlive(serializedItem);
		}

		/// <summary>
		///   Serialize the content of a handle. 
		///   Instance represents: Handle that should be serialized.
		/// </summary>
		/// <returns>Handle containing the serialized item.</returns>
		// Token: 0x060005CE RID: 1486 RVA: 0x00020F04 File Offset: 0x0001F104
		public HSerializedItem SerializeHandle()
		{
			IntPtr proc = HalconAPI.PreCall(2138);
			base.Store(proc, 0);
			HalconAPI.InitOCT(proc, 0);
			int num = HalconAPI.CallProcedure(proc);
			HSerializedItem result;
			num = HSerializedItem.LoadNew(proc, 0, num, out result);
			HalconAPI.PostCall(proc, num);
			GC.KeepAlive(this);
			return result;
		}

		/// <summary>
		///   Test if a tuple is serializable.
		///   Instance represents: Tuple to check for serializability.
		/// </summary>
		/// <returns>Boolean value indicating if the input can be serialized.</returns>
		// Token: 0x060005CF RID: 1487 RVA: 0x00020F4C File Offset: 0x0001F14C
		public int TupleIsSerializable()
		{
			IntPtr proc = HalconAPI.PreCall(2141);
			base.Store(proc, 0);
			HalconAPI.InitOCT(proc, 0);
			int num = HalconAPI.CallProcedure(proc);
			int result;
			num = HalconAPI.LoadI(proc, 0, num, out result);
			HalconAPI.PostCall(proc, num);
			GC.KeepAlive(this);
			return result;
		}

		/// <summary>
		///   Check if a handle is valid. 
		///   Instance represents: The handle to check for validity.
		/// </summary>
		/// <returns>The validity of the handle, 1 or 0.</returns>
		// Token: 0x060005D0 RID: 1488 RVA: 0x00020F94 File Offset: 0x0001F194
		public HTuple TupleIsValidHandle()
		{
			IntPtr proc = HalconAPI.PreCall(2143);
			base.Store(proc, 0);
			HalconAPI.InitOCT(proc, 0);
			int num = HalconAPI.CallProcedure(proc);
			HTuple result;
			num = HTuple.LoadNew(proc, 0, HTupleType.INTEGER, num, out result);
			HalconAPI.PostCall(proc, num);
			GC.KeepAlive(this);
			return result;
		}

		/// <summary>
		///   Return the semantic type of a tuple.
		///   Instance represents: Input tuple.
		/// </summary>
		/// <returns>Semantic type of the input tuple as a string.</returns>
		// Token: 0x060005D1 RID: 1489 RVA: 0x00020FE0 File Offset: 0x0001F1E0
		public string TupleSemType()
		{
			IntPtr proc = HalconAPI.PreCall(2144);
			base.Store(proc, 0);
			HalconAPI.InitOCT(proc, 0);
			int num = HalconAPI.CallProcedure(proc);
			string result;
			num = HalconAPI.LoadS(proc, 0, num, out result);
			HalconAPI.PostCall(proc, num);
			GC.KeepAlive(this);
			return result;
		}
	}
}
